package com.atguigu.read;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.MaxAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Es04_CodeRead {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置连接属性
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取客户端连接
        JestClient jestClient = jestClientFactory.getObject();

        //4.读取ES数据
        //通过代码代替json查询语句

        //TODO -----------------------------{}---------------------------------
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //TODO -----------------------------bool---------------------------------
        BoolQueryBuilder bool = new BoolQueryBuilder();

        //TODO -----------------------------match---------------------------------
        MatchQueryBuilder match = new MatchQueryBuilder("favo","橄榄球");

        //TODO -----------------------------must---------------------------------
        bool.must(match);

        //TODO -----------------------------term---------------------------------
        TermQueryBuilder term = new TermQueryBuilder("sex","男");

        //TODO -----------------------------filter---------------------------------
        bool.filter(term);

        //TODO -----------------------------query---------------------------------
        sourceBuilder.query(bool);

        //TODO -----------------------------terms---------------------------------
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("groupByClass").field("class_id");

        //TODO -----------------------------max---------------------------------
        MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("groupByAge").field("age");

        //TODO -----------------------------aggs---------------------------------
        sourceBuilder.aggregation(termsAggregationBuilder.subAggregation(maxAggregationBuilder));

        //TODO -----------------------------from---------------------------------
        sourceBuilder.from(0);

        //TODO -----------------------------size---------------------------------
        sourceBuilder.size(2);


        Search search = new Search.Builder(sourceBuilder.toString())
                .addIndex("student")
                .addType("_doc")
                .build();

        SearchResult result = jestClient.execute(search);

        //a.获取命中数据条数
        System.out.println(result.getTotal());

        //b.获取明细数据
        //Map.class是用来接收明细数据的
        List<SearchResult.Hit<Map, Void>> hits = result.getHits(Map.class);
        for (SearchResult.Hit<Map, Void> hit : hits) {
            System.out.println("_index:"+hit.index);
            System.out.println("_type:"+hit.type);
            System.out.println("_id:"+hit.id);
            //获取具体的明细数据
            Map source = hit.source;
            for (Object o : source.keySet()) {
                System.out.println(o+":"+source.get(o));
            }
        }

        //c.获取聚合组数据
        MetricAggregation aggregations = result.getAggregations();
        TermsAggregation groupByClass = aggregations.getTermsAggregation("groupByClass");
        List<TermsAggregation.Entry> buckets = groupByClass.getBuckets();
        for (TermsAggregation.Entry bucket : buckets) {
            System.out.println("key:"+bucket.getKey());
            System.out.println("doc_count:"+bucket.getCount());
            //获取嵌套的年龄聚合组数据
            MaxAggregation groupByAge = bucket.getMaxAggregation("groupByAge");
            System.out.println("value:"+groupByAge.getMax());
        }


        //关闭连接
        jestClient.shutdownClient();
    }
}
